/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.sms.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.sms.domain.SmsUser;
import me.zhengjie.sms.service.SmsUserService;
import me.zhengjie.sms.domain.vo.SmsUserQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.sql.Timestamp;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author nobody
* @date 2025-01-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "短信用户管理")
@RequestMapping("/api/smsUser")
public class SmsUserController {

    private final SmsUserService smsUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('smsUser:list')")
    public void exportSmsUser(HttpServletResponse response, SmsUserQueryCriteria criteria) throws IOException {
        smsUserService.download(smsUserService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询短信用户")
    @ApiOperation("查询短信用户")
    @PreAuthorize("@el.check('smsUser:list')")
    public ResponseEntity<PageResult<SmsUser>> querySmsUser(SmsUserQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(smsUserService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增短信用户")
    @ApiOperation("新增短信用户")
    @PreAuthorize("@el.check('smsUser:add')")
    public ResponseEntity<Object> createSmsUser( @RequestBody SmsUser resources){
        resources.setSmsUserToken(UUID.randomUUID().toString());
        resources.setSmsPlatformToken("f71d335e0bc92cd70d180f6fe49542f8_86848");
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        smsUserService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改短信用户")
    @ApiOperation("修改短信用户")
    @PreAuthorize("@el.check('smsUser:edit')")
    public ResponseEntity<Object> updateSmsUser(@Validated @RequestBody SmsUser resources){
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        smsUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除短信用户")
    @ApiOperation("删除短信用户")
    @PreAuthorize("@el.check('smsUser:del')")
    public ResponseEntity<Object> deleteSmsUser(@RequestBody List<Integer> ids) {
        smsUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}